package com.example.controller;

import com.example.pojo.Person;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author およそ神
 * @version JDK 1.8
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonService personService;
    /**
     * DiscoveryClient 可以用来获取一些配置的信息，得到具体的微服务！
     */
    @Autowired
    private DiscoveryClient client;
    @PostMapping("/addPerson")
    public Boolean addPerson(Person person) {
        return this.personService.addPerson(person);
    }

    @GetMapping("/searchById/{id}")
    public Person searchById(@PathVariable("id") Integer id) {
        return this.personService.searchById(id);
    }

    @GetMapping("/searchAll")
    public List<Person> searchAll() {
        return this.personService.searchAll();
    }
    /**
     * 获取一些注册进来的微服务的信息~，
     *
     * @return
     */
    @GetMapping("/person/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        // 得到一个具体的微服务信息,通过具体的微服务id，applicaioinName；
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        return this.client;
    }
}
